package tech.novastar.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.novastar.domain.Store;
import tech.novastar.utils.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StoreImpl implements IStore {
    private final static Logger logger = LoggerFactory.getLogger(StoreImpl.class);

    @Override
    public boolean addStore(Store store) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "INSERT INTO store(name, count) VALUES(?, ?)";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, store.getName());
            pstmt.setInt(2, store.getCount());
            int r = pstmt.executeUpdate();
            if(r == 1) {return true;}
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return false;
    }

    @Override
    public boolean deleteById(int id) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "DELETE FROM store WHERE id = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            int r = pstmt.executeUpdate();
            if(r == 1) {return true;}
            logger.info("删除商品{}成功", id);
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return false;
    }

    @Override
    public boolean updateById(int id, int count) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "UPDATE store set count=? WHERE id=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, count);
            pstmt.setInt(2, id);
            int r = pstmt.executeUpdate();
            if(r == 1) {return true;}
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return false;
    }

    @Override
    public List<Store> findByName(String name) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "SELECT id, name, count FROM store WHERE name like ?";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Store> stores =  new ArrayList<>();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "%" + name + "%" );
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Store store = new Store();
                store.setId(rs.getInt("id"));
                store.setName(rs.getString("name"));
                store.setCount(rs.getInt("count"));
                stores.add(store);
            }
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return stores;
    }

    @Override
    public boolean findByNameStrict(String name) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "SELECT id, name, count FROM store WHERE name = ?";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,  name);
            rs = pstmt.executeQuery();
            if (rs.next())  return true;
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return false;
    }

    @Override
    public Store findById(int id) {
        Connection conn = JdbcUtil.getConnection();
        String sql = "SELECT id, name, count FROM store WHERE id=?";
        PreparedStatement pstmt = null;
        Store store = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                store = new Store();
                store.setId(rs.getInt("id"));
                store.setName(rs.getString("name"));
                store.setCount(rs.getInt("count"));
            }
            return store;
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return store;
    }

    @Override
    public List<Store> fetchAllStore() {
        Connection conn = JdbcUtil.getConnection();
        String sql = "SELECT id, name, count from store";
        PreparedStatement pstmt = null;
        List<Store> stores = new ArrayList<>();
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Store store = new Store();
                store.setId(rs.getInt("id"));
                store.setName(rs.getString("name"));
                store.setCount(rs.getInt("count"));
                stores.add(store);
            }
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
        return stores;
    }
}
